var path;
var frame_name = "#frame_content";
var apis = {
	"getApiClass" : "getApiClass.do",
	"getApi" : "getApi.do",
	"getApistruct" : "getApistruct.do",
	"getParamstruct" : "getParamstruct.do",
	"getParametersByStruct" : "getParametersByStruct.do",
	"getErrorcode" : "getErrorcode.do",
};

var Public = Public || {};
Public.isIE6 = !window.XMLHttpRequest; // ie6

// 公共方法，POST数据,反序列化JSON返回
Public.ajaxPost = function(url, postData, callback, errCallback) {
	$.ajax({
		url : url,
		type : "POST",
		processData : true,
		data : postData,
		datatype : "json",
		success : function(data, textStatus, jqXHR) {
			var ret = null;
			if (data != undefined && jqXHR.status == 200
					&& $.trim(data).length > 0) {
				var type = typeof (data);
				if (type == "object")
					ret = data;
				else
					ret = JSON.parse(data);
			}
			callback(ret);
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			console.error(XMLHttpRequest);
			alert("错误");
			errCallback && errCallback(XMLHttpRequest);
		}
	});
};

// 公共方法，GET数据
Public.ajaxGet = function(url, params, callback, errCallback) {
	$.ajax({
		url : url,
		type : "GET",
		dataType : "json",
		data : params,
		success : callback,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			console.error(XMLHttpRequest);
			alert("错误");
			errCallback && errCallback(XMLHttpRequest);
		}
	});
};

// 公共方法，iframe换页
Public.ChangePage = function(url) {
	$(frame_name).attr('src', url);
};

// 公共方法，JS打开新页面
Public.Redirect = function(url, target) {
	var el = document.createElement("a");
	document.body.appendChild(el);
	el.href = url;
	el.target = '_blank'; // 指定在新窗口打开
	if ($.trim(target).length != 0)
		el.target = target;
	el.click();
	document.body.removeChild(el);
};

// 公共方法，当前页样式
Public.CurrentPage = function(current) {
	var pathname = current.location.pathname;
	pathname = pathname.substring(pathname.lastIndexOf("/") + 1).toLowerCase();
	pathname = '#' + pathname.substring(0, pathname.length - 5);

	var thisA = $(pathname);
	var links = $('.taggle_box ul a');
	links.removeClass('now');
	thisA.addClass('now');
};

// 公共方法，重设iframe高度(动态高度)
Public.ReSizeFrame = function() {
	/*
	 * var subWeb = $(frame_name).contents(); if (subWeb != null)
	 * $(frame_name).attr('height', subWeb.innerHeight());
	 */

	var subWeb = $(frame_name)[0].contentDocument;
	if (subWeb != null && subWeb.body != null) {
		var height = subWeb.body.scrollHeight;
		var clientheight = ((document.documentElement.clientHeight == 0) ? document.body.clientHeight
				: document.documentElement.clientHeight) - 200;
		if (height < clientheight) {
			height = clientheight;
		}

		$(frame_name).attr('height', height);
	}
};

// 公共方法，折叠文本块，内页调用
Public.Fold = function(current) {
	var button_index = $(".show").index(current);
	var $li = $(".show").parent().parent();
	if ($($li[button_index]).children(".theme_content").css("display") == "block") {
		$($li[button_index]).children(".title").children("a").html("[显示]");
		$($li[button_index]).children(".theme_content").slideUp("300");
	} else {
		$($li[button_index]).children(".title").children("a").html("[隐藏]");
		$($li[button_index]).children(".theme_content").slideDown("300");
	}
};

// 公共方法，获取URL参数
Public.GetUrlParam = function(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // 构造一个含有目标参数的正则表达式对象
	var r = window.location.search.substr(1).match(reg); // 匹配目标参数
	if (r != null) {
		// var url = unescape(r[2]);
		var url = decodeURIComponent(r[2]);
		if ($.trim(window.location.hash).length > 1) {
			url += window.location.hash;
		}
		return url;
		// return unescape(r[2]).toLowerCase();
		// return decodeURIComponent(r[2]).toLowerCase();
	} else {
		return null; // 返回参数值
	}
};

// 公共方法，搜索接口
Public.SearchAPI = function() {
	var txt = $.trim($('#txt_Search').val());
	if (txt.length == 0)
		return;
	if (txt.length > 50) {
		alert('搜索文本长度不能大于50');
		return;
	}

	Public.Redirect('SearchApi.jsp?q=' + encodeURIComponent(txt), '_blank');
};

$(function() {
	$('#txt_Search').bind('keyup', function(e) {
		var ev = e || window.event;
		if (ev.keyCode == 13) {
			Public.SearchAPI();
		}
	});
});